using System;
using System.Collections.Generic;
using System.Text;

using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Reflection;

namespace Winsmarts.PI.Common
{
    public static class Utilities
    {
        public static bool IsXmlValid(string xmlFile, string xmlSchema)
        {
            TextReader schemaStream =
                new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(xmlSchema));
            XmlSchemaSet schemaSet = new XmlSchemaSet() ;
            schemaSet.Add("",XmlReader.Create(schemaStream));

            Stream docStream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read);
            XmlReaderSettings settings = new XmlReaderSettings() ;
            settings.Schemas.Add(schemaSet) ;
            settings.ValidationType = ValidationType.Schema;
            XmlReader xRead = XmlReader.Create(docStream, settings)  ;

            bool toReturn = false;

            try
            {
                while (xRead.Read())
                {
                    // do nothing :-/
                }
                toReturn = true;
            }
            catch (XmlSchemaValidationException xExcp)
            {
                Trace.WriteLine("Input document " + xmlFile + " does not match the schema", true);
                Trace.WriteLine(xExcp.ToString(), true);
            }

            return toReturn;
        }
    }
}
